package com.woyou.ui.activity.orderlist;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.citaq.ideliver.R;
import com.woyou.BroadcastConstant;
import com.woyou.bean.MyOrderItem;
import com.woyou.ui.activity.orderdetail.State;
import com.woyou.utils.DateUtils;
import com.woyou.utils.ParseUtils;

/**
 * @author blanks
 * @创建时间 2015年9月10日 下午3:43:19
 * @version v1.0
 */
public class MultipleStatusItemView extends BaseItemView {

	private TextView shopName, orderStatus, allNum, allPrice, time;
	
	private Button button;

	private int grayColor;
	private int pinkColor;
	
	private ILoadOrderDetailListener listener;
	
	/**
	 * @param context
	 */
	public MultipleStatusItemView(Context context, ILoadOrderDetailListener listener) {
		super(context);
		this.listener = listener;
		loadLayout();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.woyou.ui.adapter.packag.BaseItemView#loadLayout()
	 */
	@Override
	void loadLayout() {
		// TODO Auto-generated method stub
		LayoutInflater.from(getContext()).inflate(R.layout.item_myorder_lv_content, this);
		shopName = (TextView) findViewById(R.id.myorder_name);
		orderStatus = (TextView) findViewById(R.id.myorder_status);
		allNum = (TextView) findViewById(R.id.myorder_allNum);
		allPrice = (TextView) findViewById(R.id.myorder_allMoney);
		time = (TextView) findViewById(R.id.myorder_time);
		button = (Button) findViewById(R.id.btnClick);
		
		grayColor = getContext().getResources().getColor(R.color.orderstate);
		pinkColor = getContext().getResources().getColor(R.color.pink_status);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.woyou.ui.adapter.packag.BaseItemView#loadData(com.woyou.bean.
	 * MyOrderItem)
	 */
	@Override
	public void loadData(MyOrderItem data) {
		int status = data.getState();
		shopName.setText(data.getsName());
		allNum.setText(data.getNum() + "份");
		allPrice.setText("￥" + ParseUtils.String2Money(data.getoPrice()));

		showOrderState(status, data);

		// if (7 == status) {
		// // 7.订单已确认，即未送出
		// // holderOrder.time.setTextColor(pinkColor);
		// time.setText("已等待" +
		// DateUtils.timeStampToFormat(data.getoTimeStamp()) + "分钟");
		//
		// } else {
		// holderOrder.time.setTextColor(grayColor);
		if (!TextUtils.isEmpty(data.oDate)
				&& data.oDate.equals(DateUtils.getTodayDate())) {
			time.setText("今天" + data.getoTime());
		} else if (!TextUtils.isEmpty(data.oDate)
				&& data.oDate.equals(DateUtils.getYestoryDate())) {
			time.setText("昨天" + data.getoTime());
		} else {
			time.setText(DateUtils.timeStampToStr(data.getoTimeStamp()));
		}
		// }

	}

	private void showOrderState(int state, final MyOrderItem data) {
		switch (state) {
		case State.NEW_ORDER:
			// 新订单
			button.setVisibility(View.GONE);
			orderStatus.setText("新订单");
			orderStatus.setTextColor(grayColor);
			break;
			
		case State.PUSH_SUCCEED:
			// 推送成功
			button.setVisibility(View.GONE);
			orderStatus.setText("等待接单");
			orderStatus.setTextColor(grayColor);
			break;
			
		case State.NOT_CONNECTED:
			// 未接通
			button.setVisibility(View.GONE);
			orderStatus.setText("未接通");
			orderStatus.setTextColor(grayColor);
			break;
			
		case State.NO_RESPONSE:
			// 接通后未应答
			button.setVisibility(View.GONE);
			orderStatus.setText("未应答");
			orderStatus.setTextColor(grayColor);
			break;
			
		case State.REFUSE:
			// 已拒绝
			button.setVisibility(View.GONE);
			orderStatus.setText("已拒绝");
			orderStatus.setTextColor(grayColor);
			break;
			
		case State.OUT_STOCK:
			// 返单
			button.setVisibility(View.GONE);
			orderStatus.setText("已返单");
			orderStatus.setTextColor(grayColor);
			break;
			
		case State.CONFIRMED:
			// 已确认
			button.setVisibility(View.VISIBLE);
			orderStatus.setText("待确认收货");
			orderStatus.setTextColor(pinkColor);
			button.setText("确认收货");
			button.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(BroadcastConstant.NOTIFY_ACT_CONFIRM_DELIVER);
					intent.putExtra(BroadcastConstant.ORDER_OID, data.getoId());
					intent.putExtra(BroadcastConstant.ORDER_ACCEPT_TIME, data.getAcceptTime());
					mContext.sendBroadcast(intent);

				}
			});
			break;
			
		case State.CANCELLED:
			// 已取消
			button.setVisibility(View.GONE);
			orderStatus.setText("已取消");
			orderStatus.setTextColor(grayColor);
			break;
			
		case State.SENT_OUT:
			// 已送出
			button.setVisibility(View.VISIBLE);
			orderStatus.setText("待确认收货");
			orderStatus.setTextColor(pinkColor);
			button.setText("确认收货");
			button.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(BroadcastConstant.NOTIFY_ACT_CONFIRM_DELIVER);
					intent.putExtra(BroadcastConstant.ORDER_OID, data.getoId());
					intent.putExtra(BroadcastConstant.ORDER_ACCEPT_TIME, data.getAcceptTime());
					mContext.sendBroadcast(intent);

				}
			});
			break;
			
		case State.DELICERED:
			// 已送达
			button.setVisibility(View.VISIBLE);
			orderStatus.setText("待评价");
			orderStatus.setTextColor(pinkColor);
			button.setText("评价订单");
			button.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(BroadcastConstant.NOTIFY_ACT_ASSESSMENT_ORDER);
					intent.putExtra(BroadcastConstant.ORDER_OID, data.getoId());
					intent.putExtra(BroadcastConstant.SHOP_SID, data.getsId());
					mContext.sendBroadcast(intent);

				}
			});
			break;
			
		case State.COMMENTED:
			// 已评价
			button.setVisibility(View.GONE);
			orderStatus.setText("已评价");
			orderStatus.setTextColor(grayColor);
			break;
			
		case State.AUTO_COMPLETE:
			// 自动完成
			button.setVisibility(View.VISIBLE);
			orderStatus.setText("待评价");
			orderStatus.setTextColor(pinkColor);
			button.setText("评价订单");
			button.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(BroadcastConstant.NOTIFY_ACT_ASSESSMENT_ORDER);
					intent.putExtra(BroadcastConstant.ORDER_OID, data.getoId());
					intent.putExtra(BroadcastConstant.SHOP_SID, data.getsId());
					mContext.sendBroadcast(intent);

				}
			});
			break;
			
		case State.CANCELLATION:
			// 申请取消中
			button.setVisibility(View.GONE);
			orderStatus.setText("申请取消中");
			orderStatus.setTextColor(grayColor);
			break;
			
		case State.NEW_ORDER_ONLINEPAY:
			//待支付
			button.setVisibility(View.VISIBLE);
			orderStatus.setText("待支付");
			orderStatus.setTextColor(pinkColor);
			button.setText("我要支付");
			button.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if(listener!=null){
						listener.loadOrderDetail(data);
					}
				}
			});
			break;
		
		case State.CANCELLED_ONLINEPAY:
			// 未支付被取消
			button.setVisibility(View.GONE);
			orderStatus.setText("已取消");
			orderStatus.setTextColor(grayColor);
			break;
			
		case State.PAID:
			// 已支付
			button.setVisibility(View.GONE);
			orderStatus.setText("等待接单");
			orderStatus.setTextColor(grayColor);
			break;
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}
	
	public interface ILoadOrderDetailListener{
		void loadOrderDetail(MyOrderItem orderItem);
	}

}
